package com.deepbreath.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.deepbreath.entity.CitySite;
import com.deepbreath.mapper.CitySiteMapper;
import com.deepbreath.service.ICitySiteService;
import com.deepbreath.spider.pm25in.CitySiteProcessor;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;

@Service
public class CitySiteServiceImpl extends ServiceImpl<CitySiteMapper, CitySite> implements ICitySiteService {

	@Resource
	private CitySiteMapper citySiteMapper;

	@Override
	public boolean doCitySiteSpider() {
		CitySiteProcessor processor = new CitySiteProcessor();
		processor.initialize(null);// 初始化
		processor.start();// 启动

		Collection<CitySite> collection = processor.getDATA_MAP();
		System.out.println("任务执行完成,结果数量:" + collection.size());

		// 插入完成后插入数据库
		boolean flag = false;
		if (!collection.isEmpty()) {
			List<CitySite> citySites = new ArrayList<CitySite>();
			citySites.addAll(collection);
			flag = this.insertBatch(citySites);
		}

		processor.shutdown();// 关闭
		return flag;
	}

}